package com.android.baseconfig.common.utils;


import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;

import com.android.baseconfig.R;
import com.android.baseconfig.base.BaseConfigApplication;

/**
 * 手机APP与云平台通讯错误码
 *
 * @author z00218627
 */
public final class ErrorCode {
    private ErrorCode() {
    }

    ;
    public static final String NO_AUTHORITY_ERROR_CODE = "190";
    // 操作成功
    public final static String ERROR_OK = "0";

    // ONT请求失败,或者某些接口ONT侧还未设置时返回1
    public final static String ERROR_NOT_CONFIGURED = "1";

    // 未知错误
    public final static String ERROR_UNDEFINED = "undefined";

    /**
     * 网络连接失败
     */
    public final static String NETWORK_ERR = "network_err";

    // 操作失败
    public final static String ERROR_FAILED = "999";

    /**
     * 云平台返回7的错误码
     */
    public final static String ERROR_7 = "7";

    /**
     * 云平台返回167的错误码
     */
    public final static String ERROR_167 = "167";

    // 用户名已存在
    public final static String ERROR_USEREXIST = "001";

    // 两次密码不相同
    public final static String ERROR_TWOPASSWORD = "002";

    // 用户名或密码错误
    public final static String ERROR_USERNAME = "003";

    // 邮箱地址不合法
    public final static String ERROR_EMAILADDRESS = "004";

    // 用户密码必须是6到16位数,字母和数字组合
    public final static String ERROR_005 = "005";

    // 手机号不正确
    public final static String ERROR_006 = "006";

    // 用户名不能为空
    public final static String ERROR_007 = "007";

    // 用户昵称已被使用
    public final static String ERROR_008 = "008";

    // 手机号已被使用
    public final static String ERROR_009 = "009";

    // 验证码错误
    public final static String ERROR_010 = "010";

    // 邮箱已被绑定
    public final static String ERROR_011 = "011";

    // token无效
    public final static String ERROR_INEFFECTUAL_TOKEN = "012";

    // 用户名或密码错误
    public final static String ERROR_013 = "013";

    // 当前用户所绑定终端个数已达上限
    public final static String ERROR_014 = "014";

    // 终端已绑定用户个数已达上限
    public final static String ERROR_015 = "015";

    // 无效的终端mac地址
    public final static String ERROR_INEFFECTUAL_MAC = "016";

    // 无效的用户账号
    public final static String ERROR_017 = "017";

    // 无效的插件ID
    public final static String ERROR_018 = "018";

    // 无效的插件操作
    public final static String ERROR_019 = "019";

    // 终端不在线
    public final static String ERROR_DEVICE_OFFLINE = "020";

    /**
     * 网关不在线另外一个错误码
     */
    public final static String ERROR_DEVICE_OFFLINE_30000118 = "30000118";

    // 对终端请求无权限
    public final static String ERROR_021 = "021";

    // 终端未注册云平台
    public final static String ERROR_022 = "022";

    // 用户名不存在
    public final static String ERROR_023 = "023";

    // 插件symbolicName不合法
    public final static String ERROR_024 = "024";

    // 数据库操作失败
    public final static String ERROR_025 = "025";

    // 当前操作插件不存在
    public final static String ERROR_026 = "026";

    // clientID不能为空
    public final static String ERROR_027 = "027";

    // clientID无效
    public final static String ERROR_028 = "028";

    // 该账号未绑定ONT
    public final static String ERROR_029 = "029";

    // 该ONT已被此账号绑定
    public final static String ERROR_030 = "030";

    // 错误码缺失
    // public final static String ERROR_031 = "031";
    // 该ONTi没绑定此账号
    public final static String ERROR_032 = "032";

    // 该ONT正在被操作
    public final static String ERROR_033 = "033";

    // 插件操作失败
    public final static String ERROR_034 = "034";

    // 插件列表查询失败
    public final static String ERROR_035 = "035";

    // 插件图片查询失败
    public final static String ERROR_036 = "036";

    // 插件版本信息查询失败
    public final static String ERROR_037 = "037";

    // WIFI覆盖信息查询失败
    public final static String ERROR_038 = "038";

    // 手机号未绑定账号
    public final static String ERROR_039 = "039";

    // 该邮箱未绑定账号
    public final static String ERROR_040 = "040";

    // 原密码输入错误
    public final static String ERROR_041 = "041";

    // 获取UDP反向连接锁失败
    public final static String ERROR_042 = "042";

    // 发送TCP消息超时
    public final static String ERROR_043 = "043";

    // 响应参数错误
    public final static String ERROR_044 = "044";

    // 获取TCP连接失败
    public final static String ERROR_045 = "045";

    // 获取UDP连接失败
    public final static String ERROR_046 = "046";

    // 家长控制列表查询失败
    public final static String ERROR_047 = "047";

    // 设备模板操作失败
    public final static String ERROR_048 = "048";

    // 您所设定的家长控制过多
    public final static String ERROR_049 = "049";

    // 无效参数
    public final static String ERROR_050 = "050";

    // 模板不存在
    public final static String ERROR_051 = "051";

    // 终端未注册
    public final static String ERROR_052 = "052";

    // 家长控制特性不支持
    public final static String ERROR_053 = "053";

    // 模板正在使用中
    public final static String ERROR_054 = "054";

    // 家长控制未知错误
    public final static String ERROR_055 = "055";

    public static final String INVALID_ACCOUNT_TYPE_ERROR = "066"; // 无效账号类型

    public static final String PWD_NULL_ERROR = "067"; // 密码不能为空

    public static final String REPWD_NULL_ERROR = "068"; // 确认密码不能为空

    public static final String FAMILYNAME_NULL_ERROR = "069"; // 家庭账号不能为空

    public static final String PPPOEACCOUNT_ERROR = "070"; // PPOE账号错误

    public static final String INTELLISENSEONT_ERROR = "071"; // 为智能ONT

    public static final String TYACCOUNT_NULL_ERROR = "072"; // 天翼云账号为空

    public static final String INVALID_TYACCOUNT_TYPE_ERROR = "073"; // 天翼云账号类型错误

    public static final String USER_EXSIT_FAMILIYACCOUNT_ERROR = "074"; // 天翼云家庭账号已注册

    public static final String USER_EXSIT_TYACCOUNT_ERROR = "075"; // 天翼云账号已注册

    public static final String USER_NOTEXSIT_TYACCOUNT_ERROR = "076"; // 天翼云账号不存在

    public static final String PPPOE_ACCOUNT_NULL = "077"; // PPOE账号为空

    public static final String MAC_UNREGISTER_ERROR = "078"; // PPOE账号为空

    public static final String INVALID_FAMILY_ID = "079"; // 无效的familyID

    public static final String NULL_FAMILY_ERROR = "080"; // 当前账号不输入任何家庭

    public static final String BIND_RELATION_NOT_EXIST = "081"; // 当前账号不输入任何家庭

    public static final String ACCOUNT_NO_EXIST_ERROR = "082"; // 用户账号不存在

    public static final String FAMILY_NULL_ERROR = "083"; // 家庭ID不能为空

    public static final String ACCOUNT_IS_NOT_IN_FAMILY = "084"; // 当前将要删除的家人已经不属于该家庭

    public static final String INVALID_BIND_TYPE = "089"; // 无效的绑定类型

    public static final String INVALID_CREATEFAMILY_TYPE = "090"; // 无效的创建家庭类型

    public static final String INVALID_UNBIND_TYPE = "091"; // 无效的绑定类型

    public static final String OVER_MAX_FAMILY_MEMBER_NUM = "092"; // 邀请的家庭成员已超限

    public static final String CHECK_PPPOE_IS_NULL = "093"; // 未绑定PPPoE账号

    public static final String EXCEED_MAX_CREATE_FAMILY_NUM = "094"; // 超过同一个账户创建家庭数量的最大配置值

    public static final String EXCEED_MAX_DELETE_FAMILY_NUM = "095"; // 家庭最后一个成员不让删除

    public static final String DELETE_FAMILY_MEMBER_LIST_NULL = "096"; // 待删除家人列表为空

    public static final String PPPOE_CHECK_UNAVILABLE = "097"; // 暂时不支持PPPOE账号校验

    public static final String PLACE_NAME_EXIST = "099"; // WIFI覆盖名称已存在

    public static final String PASS_WILL_EXPRIRES = "103"; // 密码即将超期

    public static final String PASS_IS_EXPRIRES = "104"; // 密码已经超期

    public static final String USER_IS_LOCKED = "105"; // 用户已经被锁定

    public static final String ACCOUNT_REATCH_MAX = "107"; // 登录账号达到上限值

    public static final String PASSWORD_COMPLEX_INVALID = "108"; // 密码复杂度不够

    public static final String OLD_PASSWORD_ERROR = "116"; // 修改密码时，旧密码错误

    public static final String ACCOUNT_EXSITS = "131"; // 修改账号是，账号已存在

    // ONT的版本是最新的
    public static final String ONT_VERSION_IS_NEWLY = "141";

    // ONT的版本是最新的
    public static final String ONT_VERSION_NOT_NEED_UPDATE = "140";

    public static final String ERROR_CHECK_FAILED = "check failed"; // 修改密码时，旧密码错误

    // 找不到房间
    public static final String ROOM_NOT_FOUND = "2002";

    // 找不到该设备
    public static final String DEVICE_NOT_FOUND = "2003";

    // 设备已经存在
    public static final String DEVICE_EXISTED = "2004";

    // 没有购买该类设备服务
    public static final String NOT_HAVE_THIS_TAOCAN = "2005";

    // 套餐未激活
    public static final String TAOCAN_NOT_ALIVE = "2006";

    // 设备数量超出套餐限制
    public static final String EXCEED_TAOCAN = "2007";

    // 名称已经存在
    public static final String NAME_EXISTED = "2008";

    // 查询套餐状态失败。
    public static final String GET_TAOCAN_STATUS_FAILED = "2009";

    /**
     * 用户的手机号绑定了多个账号
     */
    public static final String PHONE_BIND_MUCH_ACCOUNT = "239";

    /**
     * 用户的邮箱绑定了多个账号
     */
    public static final String EMAIL_BIND_MUCH_ACCOUT = "240";

    /**
     * 用户的账号同时绑定了手机号和邮箱
     */
    public static final String ACCOUNT_BIND_PHONE_AND_EMAIL = "241";

    /**
     * 未订购APP业务
     */
    public static final String NO_PURCHASE_APP = "251";

    public static final String OUT_OF_ROOM_MAX_NUM = "80013001";

    /**
     * <p>
     * 错误的返回码
     * </p>
     */
    public final static String ERROR = "-1";

    /**
     * ONT 不支持SSL时的错误码
     */
    public final static String ONT_SSL_FAILED = "911";

    public final static String HTTP_STATUS_NOT_FOUND = "404";

    private static final String TAG = ErrorCode.class.getName();

    /**
     * <br>
     *
     * @param errCode
     * @return
     * @see [根据错误码显示错误信息]
     * @since LinkHome, 2015-11-30
     */
    public static String getErrorMsg(String errCode) {
        Context context = BaseConfigApplication.getInstance();
        return getErrorMsg(context, errCode);
    }

    /**
     * <br>
     *
     * @param context
     * @param errCode
     * @return
     * @see [根据错误码显示错误信息]
     * @since LinkHome, 2015-11-30
     */
    public static String getErrorMsg(Context context, String errCode) {
        String tempErrCode = errCode;

        if (!StringUtils.isEmpty(tempErrCode) && tempErrCode.startsWith("-")) {
            tempErrCode = tempErrCode.replace("-", "_");
        }

        Resources res = context.getResources();
        if ("undefined".equals(tempErrCode)) {
            tempErrCode = "999";
        }
        int resId = 0;
        try {
            resId = res.getIdentifier("error_" + tempErrCode, "string", context.getPackageName());
        } catch (Exception e) {
            // TODO: handle exception
            Logc.e(TAG, "Exception====>", e);
        }


        if (resId == 0) {
            return res.getString(R.string.operate_failed) + "(" + errCode + ")";
        }

        String result = "";
        try {
            result = res.getString(resId);
        } catch (NotFoundException e) {
            result = res.getString(R.string.operate_failed) + "(" + errCode + ")";
            Logc.e(TAG, "NotFoundException====>", e);
        }

        return result;
    }
}